﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.BPW
{
    [Description("动态表单，动态创建列并绑定数据,用于审核专门审核销售订单"), HotUpdate]
    public class SalListToAudit : AbstractDynamicFormPlugIn
    {
        #region var
        /// <summary>
        /// 单据体标识
        /// </summary>
        private const string entityKey = "F_POSW_Entity_qtr";
        /// <summary>
        /// 需要动态创建的列的列名
        /// </summary>
        private string[] fieldNames = { "编码", "名称", "备注" };
        #endregion
        #region event
        /// <summary>
        /// 动态创建表格列并绑定数据
        /// </summary>
        /// <param name="e"></param>
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            BindData();
        }
        /// <summary>
        /// 主菜单点击事件
        /// </summary>
        /// <param name="e"></param>
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("tbRefresh"))
            {
                //BindData();
                var obj = this.View.Model.DataObject;
                var v = this.View;
                var m = this.Model;
                Entity entity = this.View.BillBusinessInfo.GetEntity(entityKey);
                var rowsObj = this.View.Model.BillBusinessInfo.GetEntryEntity(entityKey);
                var vo = this.View.Model.GetEntityDataObject(entity);
                var o = this.Model.GetEntityDataObject(entity);
                DynamicObjectCollection rows = obj["POSW_Kec89351c"] as DynamicObjectCollection;
                foreach (var item in rows)
                {

                }
            }
        }
        /// <summary>
        /// 动态创建表格列并绑定数据
        /// </summary>
        private void BindData()
        {
            //var columnNames = this.View.Model.GetValue("F_Jac_Text") as string;
            //if (!string.IsNullOrWhiteSpace(columnNames))
            //{
            //    fieldNames = columnNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //}
            //else
            //{

            //}
            fieldNames = new[] { "编码", "名称", "备注" };
            var actionData = new JSONObject();
            actionData["key"] = entityKey;
            actionData["methodname"] = "CreateDyanmicList";
            JSONArray coMeta = new JSONArray();
            //添加需要创建的列
            coMeta.Add(GetColumns());
            coMeta.Add("");
            actionData["args"] = coMeta;
            this.View.AddAction("InvokeControlMethod", actionData, x =>
            {
                //单据体动态创建列之后通过回调方式为该单据体添加数据
                var grid = this.View.GetControl<EntryGrid>(entityKey);
                grid.SetData(GetEntityData());
            });
        }
        /// <summary>
        /// 生成动态列指令集
        /// </summary>
        /// <returns></returns>
        private JSONObject GetColumns()
        {
            // 生成构建表格的指令集
            var entityAppearance = this.View.LayoutInfo.GetEntityAppearance(entityKey);
            var gridMeta = entityAppearance.CreateControl();
            gridMeta.Put("primaryKey", ExtConst.GRID_INDEX_FIELD_KEY);
            gridMeta.Put("indexKey", ExtConst.GRID_INDEX_FIELD_KEY);
            gridMeta.Put("startRow", 0);
            gridMeta.Put("pageSize", entityAppearance.PageRows);
            //gridMeta.Put("editable", false);
            // 生成列
            var columns = (JSONArray)gridMeta["columns"];
            for (var i = 0; i < fieldNames.Length; ++i)
            {
                var col = new JSONObject();
                var fieldName = fieldNames[i];
                string xtype = "textfield";
                col.Put("xtype", xtype);
                col.Put("colIndex", i + 2);
                col.Put("dataIndex", fieldName);
                col.Put("header", fieldName);
                col.Put("width", "300");
                col.Put("visible", true);
                if (fieldName == "名称")
                {
                    // 如果需要某列的单元格可编辑，必须设置该列的编辑控件属性
                    col.Put("editor", CreateEditorControl(xtype));
                }
                columns.Add(col);
            }
            return gridMeta;
        }
        /// <summary>
        /// 把元素对应的编辑控件输出为Json对象
        /// </summary>
        /// <returns></returns>
        private JSONObject CreateEditorControl(string xtype)
        {
            var ctlRet = new JSONObject();
            ctlRet.Put("xtype", xtype);
            ctlRet.Put("editable", true);
            return ctlRet;
        }
        /// <summary>
        /// 生成表格数据        
        /// </summary>
        /// <returns></returns>
        private JSONObject GetEntityData()
        {
            var data = new JSONObject();
            var rows = new JSONArray();
            for (var x = 1; x <= 20; ++x)
            {
                var row = new JSONArray();
                row.Add(x);
                row.Add(x);
                foreach (var fieldName in fieldNames)
                {
                    row.Add(string.Format("{0}：{1}", fieldName, Guid.NewGuid()));
                }
                rows.Add(row);
            }
            data.Put("rows", rows);

            return data;
        }
        #endregion
    }
}